package com.anxinm.factory;

import com.alibaba.fastjson.JSON;
import com.anxinm.excetions.SenoxContractException;
import com.anxinm.factory.platform.ContractPlatFormHandle;
import com.anxinm.pojo.entity.ContractConfig;
import com.sun.xml.internal.ws.client.SenderException;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Set;


@Slf4j
@Scope("prototype")
@Component("ContractPlatFormInterfaceHandleFactory")
public class ContractPlatFormHandleFactory {

    @Autowired
    private BeanFactory beanFactory ;

    /**字段资源 map 只初始化一次，不需要考虑并发问题*/
    public static final Map<String,String> CONFIG_FIELD_MAP = new HashMap<>() ;

    /**初始化资源*/
    static {
        CONFIG_FIELD_MAP.put("Url","") ;
        CONFIG_FIELD_MAP.put("Ip","") ;
        CONFIG_FIELD_MAP.put("AppId","") ;
        CONFIG_FIELD_MAP.put("AppSecret","") ;
    }

    /**
     * 延迟加载工厂类
     * */
    public ContractPlatFormHandleFactory(){
        try {
            Thread.sleep(500);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 初始化环境
     * @param envKey {@link com.anxinm.constant.FaDaDaConstant}
     * @throws IllegalAccessException
     * @throws NoSuchFieldException
     */
    private ContractConfig init(String envKey){
        if (StringUtils.isBlank(envKey)) {
            throw new SenderException("环境值不能为空!") ;
        }
        /**获取定义好的 key */
        Set<String> sourceKey = CONFIG_FIELD_MAP.keySet() ;
        Class<? extends Object> aClass = beanFactory.getBean("FaDaDaConfiguration").getClass() ;
        if (ObjectUtils.isEmpty(aClass)) {
            throw new SenoxContractException("加载环境配置失败!") ;
        }
        /**加载字段  map  */
        for (String key : sourceKey) {
            try {
                /**
                 * 通过反射来获取当前对象的资源是否加载
                 * 若没有加载则跳过当前循环
                 * 若加载成功
                 * 则把资源放到  map 中
                 * */
                Object data = aClass.getDeclaredField(envKey + key).get(aClass) ;
                if (Objects.isNull(data)) {
                    continue ;
                }else {
                    CONFIG_FIELD_MAP.put(key,data.toString()) ;
                }
            } catch (IllegalAccessException | NoSuchFieldException e) {
                e.printStackTrace() ;
                throw new SenoxContractException(e.getMessage()) ;
            }
        }
        /**map转json再转为实体类*/
        return JSON.parseObject(JSON.toJSONString(CONFIG_FIELD_MAP), ContractConfig.class) ;
    }

    /**
     * 获取合同平台操作处理类
     * @param envKey {@link com.anxinm.constant.FaDaDaConstant}
     * @param platFormKey {@link com.anxinm.constant.FaDaDaConstant}
     * @return {@link ContractPlatFormHandle}
     */
    public <T> T getPlatFormHandle(String platFormKey ,String envKey ,Class<T> tClass) {
        /**加载平台环境*/
        ContractPlatFormHandle formHandle = beanFactory.getBean(platFormKey, ContractPlatFormHandle.class) ;
        /**获取环境配置*/
        ContractConfig config = this.init(envKey) ;
        /**加载平台配置*/
        formHandle.initPlatForm(config) ;
        return (T) formHandle ;
    }

}
